<?php
/**
 * Created by JetBrains PhpStorm.
 * User: thanh
 * Date: 4/12/12
 * Time: 8:22 AM
 * To change this template use File | Settings | File Templates.
 */
class TrainingController extends Controller
{
    private $_model;

    public function actions()
    {
        return array(
            'captcha' => array(
                'class'=>'CCaptchaAction',
                'backColor'=>0xFFFFFF,
            ),
        );
    }

    public function actionIndex()
    {
        $criteria = new CDbCriteria(array(
            'condition' => 'status=1',
            'order' => 'create_time DESC',
            //'with'=>'commentCount',
        ));
        /*if(isset($_GET['tag']))
            $criteria->addSearchCondition('tags',$_GET['tag']);*/

        $dataProvider = new CActiveDataProvider('Training', array(
            'pagination' => array(
                'pageSize' => Yii::app()->params['postsPerPage'],
            ),
            'criteria' => $criteria,
        ));


        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
        //$this->render('index');
    }

    public function actionView()
    {
        $model = $this->loadModel();
        $comment = $this->newComment($model);

        $this->render('view', array('model' => $model, 'comment' => $comment));
    }

    public function actionRegister()
    {
        $this->layout = 'no';

        if (isset($_GET['cid'])) {
            $model = new RegisterClassForm();
            $model->cid = $_GET['cid'];
            if (isset($_POST['ajax']) && $_POST['ajax'] === 'register-class-form') {
                echo CActiveForm::validate($model);
                Yii::app()->end();
            }
            if (isset($_POST['RegisterClassForm'])) {
                $model->attributes = $_POST['RegisterClassForm'];

                if ($model->validate()) {
                    $arrDate = explode('/', $model->birthday);
                    $model->birthday = mktime(0, 0, 0, $arrDate[1], $arrDate[0], $arrDate[2]);
                    //var_dump($model->attributes);
                    //var_dump($model);die;
                    $ok = $model->register();
                    header('Content-Type: application/json');
                    echo json_encode(array('ok' => $ok));
                    Yii::app()->end();
                }
            } else {
                $gclass = GuitarClass::model()->findByPk($_GET['cid']);
                $this->render('register', array('model' => $model, 'gclass' => $gclass));
            }


        }
    }

    public function actionViewclass(){
        if(isset($_GET['id'])){
            $id = (int)$_GET['id'];
            $gclass = GuitarClass::model()->findByPk($id);
            $this->render('viewclass',array('model'=>$gclass));
        }
    }
    public function actionViewfacility(){
        if(isset($_GET['fid'])){
            $id = (int)$_GET['fid'];
            $gclass = Facility::model()->findByPk($id);
            $this->render('viewfacility',array('model'=>$gclass));
        }
    }
    public function loadModel()
    {
        if ($this->_model === null) {
            if (isset($_GET['vid'])) {
                $condition = '';
                $this->_model = Training::model()->findByPk($_GET['vid'], $condition);
            }
            if ($this->_model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
        }
        return $this->_model;
    }

    /**
     * @param $trainig Training
     * @return Comments
     */
    protected function newComment($trainig)
    {
        $comment = new Comments;
        if (!Yii::app()->user->isGuest) {
            $comment->user_id = (int)Yii::app()->user->id;
            //            $comment->item_type = 'training';
            //$comment->item_id = $trainig->id;
        }
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'comments-form') {
            echo CActiveForm::validate($comment);
            Yii::app()->end();
        }
        if (isset($_POST['Comments'])) {
            $comment->attributes = $_POST['Comments'];
            if ($trainig->addComment($comment)) {
                Yii::app()->user->setFlash('commentSubmitted', 'Cảm ơn bạn đã bình luận.');
                $this->refresh();
            }
        }
        return $comment;
    }
}
